package service4admin

import (
	"Gin-Gorm-boke/boke_admin/models4admin"
	"Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/jinzhu/copier"
	"strconv"
)

type ArticleCo struct {
	Id         int      `gorm:"column:id;" json:"id"`
	Title      string   `gorm:"column:title;" json:"title"`
	Content    string   `gorm:"column:content;" json:"content"`
	Summary    string   `gorm:"column:summary;" json:"summary"`
	CategoryId null.Int `gorm:"column:category_id;" json:"categoryId"`
	Thumbnail  string   `gorm:"column:thumbnail;" json:"thumbnail"`
	IsTop      string   `gorm:"column:is_top;default:'0';" json:"isTop"`
	Status     string   `gorm:"column:status;default:'1';" json:"status"`
	IsComment  string   `gorm:"column:is_comment;default:'1';" json:"isComment"`
	Tags       []int    `json:"tags"`
}

func SaveArticle(c *gin.Context) {
	var articleCo ArticleCo
	var article models4admin.Article
	if err := c.ShouldBindJSON(&articleCo); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	copier.Copy(&article, &articleCo)
	fmt.Println(article)
	models.Db.Model(new(models4admin.Article)).Create(&article)

	for _, v := range articleCo.Tags {
		var articleTag models4admin.ArticleTag
		articleTag.ArticleId = article.Id
		articleTag.TagId = v
		models.Db.Model(new(models4admin.ArticleTag)).Create(&articleTag)
	}
	response.ResponseOK(c, nil)
	return
}

func GetArticleList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	title := c.Query("title")
	summary := c.Query("summary")
	db := models.Db.Model(new(models4admin.Article))
	if title != "" {
		db.Where("title like ?", "%"+title+"%")
	}
	if summary != "" {
		db.Where("summary like ?", "%"+summary+"%")
	}
	var total int64
	db.Count(&total)
	var articleVoList []models4admin.ArticleVo
	db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&articleVoList)
	response.ResponseOK(c, gin.H{
		"total": total,
		"rows":  articleVoList,
	})
	return

}

func UpdateArticle(c *gin.Context) {
	var articleDto models4admin.ArticleDto
	var articleWithDetail models4admin.ArticleWithDetail

	if err := c.ShouldBindJSON(&articleWithDetail); err != nil {
		response.ResponseBadRequest(c)
		return
	}

	copier.Copy(&articleDto, &articleWithDetail)
	var tags = articleWithDetail.Tags
	models.Db.Model(new(models4admin.Article)).Where("id=?", articleDto.Id).Updates(&articleDto)
	if articleDto.DelFlag.Valid {
		models.Db.Model(new(models4admin.Article)).Where("id=?", articleDto.Id).Update("del_flag", articleDto.DelFlag.Int64)
	}
	if articleDto.ViewCount.Valid {
		models.Db.Model(new(models4admin.Article)).Where("id=?", articleDto.Id).Update("view_count", articleDto.ViewCount.Int64)
	}
	sql := "delete from sg_article_tag where article_id=?"
	models.Db.Exec(sql, articleDto.Id)
	for _, v := range tags {
		var articleTag models4admin.ArticleTag
		articleTag.ArticleId = articleDto.Id
		articleTag.TagId = v
		models.Db.Model(new(models4admin.ArticleTag)).Create(&articleTag)
	}
	response.ResponseOK(c, nil)
	return

}

func GetArticleById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var articleVo models4admin.ArticleVo
	models.Db.Where("id=?", id).First(&articleVo)
	var tags []int
	models.Db.Model(new(models4admin.ArticleTag)).
		Select("tag_id").
		Where("article_id=?", id).
		Find(&tags)
	var articleWithDetail models4admin.ArticleWithDetail
	copier.Copy(&articleWithDetail, &articleVo)
	articleWithDetail.Tags = tags

	response.ResponseOK(c, articleWithDetail)
	return
}

func DelArticleById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	models.Db.Where("id=?", id).Update("del_flag", 1)
	models.Db.Where("id=?", id).Delete(new(models4admin.Article))
	response.ResponseOK(c, nil)
	return
}
